package com.work.keep.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.work.keep.R;
import com.work.keep.activity.OnRecyclerItemClickListener;
import com.work.keep.activity.TopicActivity;
import com.work.keep.datas.DynamicDiscoverData;
import com.work.keep.datas.DynamicUserWallData;
import com.work.keep.datas.DynamicViewPagerData;
import com.work.keep.view.RecyclerViewViewPagerNetWorkImageView;
import com.work.keep.volley.VolleySingleton;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/11.
 */
public class MyRecyclerAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<DynamicDiscoverData.DataEntity> datas;
    private List<DynamicViewPagerData.DataEntity> pagerDataEntityLists;
    private List<DynamicUserWallData.DataEntity> userDataEntityLists;
    private ImageLoader imageLoader;
    private VolleySingleton volleySingleton;
    private MyPagerAdapter pagerAdapter;
    private MyPagerHolder holder;
    private MyViewHolder myViewHolder;
    private MyAvaterHolder myAvaterHolder;
    List<NetworkImageView> networkImageViews;

    public MyRecyclerAdapter(Context context) {
        this.context = context;
        init();

    }

    private OnRecyclerItemClickListener onRecyclerItemClickListener;

    public void setOnRecyclerItemClickListener(OnRecyclerItemClickListener onRecyclerItemClickListener) {
        this.onRecyclerItemClickListener = onRecyclerItemClickListener;
    }

    private void init() {
        datas = new ArrayList<>();
        networkImageViews = new ArrayList<>();
        pagerDataEntityLists = new ArrayList<>();
        userDataEntityLists = new ArrayList<>();
        imageLoader = VolleySingleton.getInstance().getImageLoader();
        volleySingleton = VolleySingleton.getInstance();

    }

    public void addPagerDatas(List<DynamicViewPagerData.DataEntity> pagerDataEntityLists) {
        this.pagerDataEntityLists = pagerDataEntityLists;
        pagerAdapter.notifyDataSetChanged();
        notifyDataSetChanged();

    }

    public void addRecyclerData(List<DynamicDiscoverData.DataEntity> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void addUserWallData(List<DynamicUserWallData.DataEntity> userDataEntityLists) {
        this.userDataEntityLists = userDataEntityLists;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v;
        if (viewType == 0) {
            v = LayoutInflater.from(context).inflate(R.layout.fragment_dynamic_pager_item, parent, false);
//            if (holder == null) {
            holder = new MyPagerHolder(v);
//            }
            return holder;
        }
        if (viewType == 1) {
            v = LayoutInflater.from(context).inflate(R.layout.fragment_dynamic_avater_item, parent, false);
//            if (myAvaterHolder == null) {
            myAvaterHolder = new MyAvaterHolder(v);
//            }
            return myAvaterHolder;
        }
        if (viewType > 1) {
            v = LayoutInflater.from(context).inflate(R.layout.fragment_dynamic_item, parent, false);
//        if (myViewHolder == null) {
            myViewHolder = new MyViewHolder(v);
//        }
            return myViewHolder;
        }
        v = new View(context);
        return new RecyclerView.ViewHolder(v) {
            @Override
            public String toString() {
                return super.toString();
            }
        };
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

        if (position == 0) {
            bindViewPager(((MyPagerHolder) holder));
//            if (pagerAdapter == null) {

//            }
        }
        if (position == 1) {

            networkImageViews = new ArrayList<>();
            networkImageViews.add(((MyAvaterHolder) holder).cardLeftNI1);
            networkImageViews.add(((MyAvaterHolder) holder).cardLeftNI0);
            networkImageViews.add(((MyAvaterHolder) holder).cardLeftNI2);
            networkImageViews.add(((MyAvaterHolder) holder).cardLeftNI3);
            networkImageViews.add(((MyAvaterHolder) holder).cardLeftNI4);
            networkImageViews.add(((MyAvaterHolder) holder).cardLeftNI5);
            if (userDataEntityLists.size() > 0) {
                for (int i = 0; i < networkImageViews.size(); i++) {
                    networkImageViews.get(i).setDefaultImageResId(R.mipmap.placeholder41_41);
                    networkImageViews.get(i).setErrorImageResId(R.mipmap.placeholder41_41);
                    networkImageViews.get(i).setImageUrl(userDataEntityLists.get(i).getAvatar(), imageLoader);
                }
            }
            ((MyAvaterHolder) holder).cardRightTop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, TopicActivity.class);
                    context.startActivity(intent);
                }
            });
        }

        if (position > 1) {
            int pos = position - 2;
            ((MyViewHolder) holder).content.setText(datas.get(pos).getContent());
            bindNetWorkIV((MyViewHolder) holder, pos);
            //默认
            ((MyViewHolder) holder).commentsCount.setText(datas.get(pos).getComments() + "");
            ((MyViewHolder) holder).likesCount.setText(datas.get(pos).getLikes() + "");
            ((MyViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onRecyclerItemClickListener != null) {
                        onRecyclerItemClickListener.onItemClick(position);
                    }
                }
            });
        }
    }

    private void bindViewPager(MyPagerHolder holder) {
        pagerAdapter = new MyPagerAdapter(pagerDataEntityLists);
        holder.viewPager.setAdapter(pagerAdapter);

    }


    private void bindNetWorkIV(final MyViewHolder holder, int position) {
        holder.photo.setDefaultImageResId(R.mipmap.ic_launcher);
        //错误
        holder.photo.setErrorImageResId(R.mipmap.ic_launcher);
        //
        holder.photo.setImageUrl(datas.get(position).getPhoto(), imageLoader);
        final String url = datas.get(position).getPhoto();

        ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                Palette.Builder builder = Palette.from(response);
                builder.generate(new Palette.PaletteAsyncListener() {
                    @Override
                    public void onGenerated(Palette palette) {
                        Palette.Swatch virbant = palette.getVibrantSwatch();
                        Palette.Swatch darkVibrant = palette.getDarkVibrantSwatch();
                        if (virbant != null) {
                            holder.linearLayout.setBackgroundColor(virbant.getRgb());
                            holder.color = virbant.getRgb();
                        }
                        if (darkVibrant != null) {
                            holder.layout.setBackgroundColor(darkVibrant.getRgb());
                        }

                    }
                });

            }
        }, 223, 223, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton._addRequest(imageRequest);

    }


    @Override
    public int getItemCount() {
        return datas.size() + 2;
    }

    //item 2的
    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView content, likesCount, commentsCount;
        private ImageView likes, comments;
        private int color;
        private RelativeLayout layout;
        private CardView cardView;
        NetworkImageView photo;
        private LinearLayout linearLayout;

        public int getColor() {
            return color;
        }

        public MyViewHolder(View itemView) {
            super(itemView);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.cardViewLayout);
            cardView = (CardView) itemView.findViewById(R.id.cardViewFeatured);
            content = (TextView) itemView.findViewById(R.id.dynamicItemContent);
            likesCount = (TextView) itemView.findViewById(R.id.dynamicItemLikesCounts);
            commentsCount = (TextView) itemView.findViewById(R.id.dynamicItemCommentsCounts);
            photo = (NetworkImageView) itemView.findViewById(R.id.dynamicItemIv);
            likes = (ImageView) itemView.findViewById(R.id.dynamicItemLikes);
            comments = (ImageView) itemView.findViewById(R.id.dynamicItemComments);
            layout = (RelativeLayout) itemView.findViewById(R.id.dynamicItemLayout);
            color = 0;
        }

    }

    //item 1 的viewHolder
    public class MyAvaterHolder extends RecyclerView.ViewHolder {
        private CardView cardLeft, cardRightTop, cardRightBottom;
        private NetworkImageView cardLeftNI0, cardLeftNI1, cardLeftNI2, cardLeftNI3, cardLeftNI4, cardLeftNI5;

        public MyAvaterHolder(View itemView) {
            super(itemView);
            cardLeft = (CardView) itemView.findViewById(R.id.cardLeft);
            cardRightBottom = (CardView) itemView.findViewById(R.id.cardRightBottom);
            cardRightTop = (CardView) itemView.findViewById(R.id.cardRightTop);
            cardLeftNI0 = (NetworkImageView) itemView.findViewById(R.id.cardLeftNI0);
            cardLeftNI1 = (NetworkImageView) itemView.findViewById(R.id.cardLeftNI1);
            cardLeftNI2 = (NetworkImageView) itemView.findViewById(R.id.cardLeftNI2);
            cardLeftNI3 = (NetworkImageView) itemView.findViewById(R.id.cardLeftNI3);
            cardLeftNI4 = (NetworkImageView) itemView.findViewById(R.id.cardLeftNI4);
            cardLeftNI5 = (NetworkImageView) itemView.findViewById(R.id.cardLeftNI5);

        }
    }

    /**
     * A adapter for avaters in the userwall cardview;
     */
//    class MyAvaterAdapter extends RecyclerView.Adapter<MyAvaterAdapter.MyAvaterViewHolder> {
//        private MyAvaterViewHolder myAvaterViewHolder;
//
//        @Override
//        public MyAvaterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
////            if (myAvaterViewHolder == null) {
//            View v = LayoutInflater.from(context).inflate(R.layout.fragment_dynamic_avater_recycler_item, parent, false);
//            myAvaterViewHolder = new MyAvaterViewHolder(v);
////            }
//            return myAvaterViewHolder;
//        }
//
//        @Override
//        public void onBindViewHolder(MyAvaterViewHolder holder, int position) {
//            holder.avaterUserWall.setDefaultImageResId(R.mipmap.ic_launcher);
//            holder.avaterUserWall.setErrorImageResId(R.mipmap.ic_launcher);
//            holder.avaterUserWall.setImageUrl(userDataEntityLists.get(position).getAvatar(), imageLoader);
//        }
//
//        @Override
//        public int getItemCount() {
//            return userDataEntityLists.size();
//        }
//
//        class MyAvaterViewHolder extends RecyclerView.ViewHolder {
//            private NetworkImageView avaterUserWall;
//
//            public MyAvaterViewHolder(View itemView) {
//                super(itemView);
//                avaterUserWall = (NetworkImageView) itemView.findViewById(R.id.recyclerAvaterNI);
//            }
//        }
//    }
//    class MyAvaterHolder extends RecyclerView.ViewHolder {
//        private CardView cardLeft, cardRightTop, cardRightBottom;
//        private NetworkImageView avater0, avater1, avater2, avater3, avater4, avater5;
//
//        public MyAvaterHolder(View itemView) {
//            super(itemView);
////            avater0 = (NetworkImageView) itemView.findViewById(R.id.dynamicAvatersIV0);
////            avater1 = (NetworkImageView) itemView.findViewById(R.id.dynamicAvatersIV1);
////            avater2 = (NetworkImageView) itemView.findViewById(R.id.dynamicAvatersIV2);
////            avater3 = (NetworkImageView) itemView.findViewById(R.id.dynamicAvatersIV3);
////            avater4 = (NetworkImageView) itemView.findViewById(R.id.dynamicAvatersIV4);
////            avater5 = (NetworkImageView) itemView.findViewById(R.id.dynamicAvatersIV5);
//
//            cardLeft = (CardView) itemView.findViewById(R.id.cardLeft);
//            cardRightBottom = (CardView) itemView.findViewById(R.id.cardRightBottom);
//            cardRightTop = (CardView) itemView.findViewById(R.id.cardRightTop);
//        }
//    }

    public class MyPagerHolder extends RecyclerView.ViewHolder {
        private ViewPager viewPager;


        public MyPagerHolder(View itemView) {
            super(itemView);
            viewPager = (ViewPager) itemView.findViewById(R.id.dynamicItemViewPager);
        }
    }

    class MyPagerAdapter extends PagerAdapter {

        private MyPagerViewHolder myPagerViewHolder;
        private List<DynamicViewPagerData.DataEntity> pagerDataEntityLists;
        private int color = 0;

        public MyPagerAdapter(List<DynamicViewPagerData.DataEntity> pagerDataEntityLists) {
            this.pagerDataEntityLists = pagerDataEntityLists;
        }


        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            if (container != null) {
                container.removeView((View) object);
            }
        }

        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            View v = LayoutInflater.from(context).inflate(R.layout.fragment_dynamic_pager_iv, container, false);
            myPagerViewHolder = new MyPagerViewHolder(v);
            myPagerViewHolder.networkImageView.setDefaultImageResId(R.mipmap.placeholder292_292);
            myPagerViewHolder.networkImageView.setErrorImageResId(R.mipmap.placeholder292_292);
            int size = pagerDataEntityLists.size();
            if (size > 0) {
                myPagerViewHolder.networkImageView.setImageUrl(pagerDataEntityLists.get(position % size).getPhoto(), imageLoader);
            }

            container.addView(v);
            return v;
        }


        class MyPagerViewHolder {
            RecyclerViewViewPagerNetWorkImageView networkImageView;

            public MyPagerViewHolder(View itemView) {
                this.networkImageView = (RecyclerViewViewPagerNetWorkImageView) itemView.findViewById(R.id.dynamicItemVPNI);
            }
        }
    }


}